package com.formulas.certification.oauth.gatewayimpl;

import cn.hutool.core.util.IdUtil;
import com.formulas.certification.domain.oauth.gateway.OauthClientDetailsGateway;
import com.formulas.certification.domain.oauth.model.OauthClientDetailsEntity;
import com.formulas.certification.dto.OauthClientDetailsQry;
import com.formulas.certification.oauth.dao.OauthClientDetailsDAO;
import com.formulas.certification.oauth.entity.OauthClientDetails;
import com.formulas.certification.oauth.mapper.OauthClientDetailsCustomMapper;
import com.formulas.certification.oauth.service.OauthClientDetailsService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author yiwannuofulasi
 * @date 2023/6/13 17:22
 */
@Component
public class OauthClientDetailsGatewayImpl implements OauthClientDetailsGateway {

    @Resource
    private OauthClientDetailsService oauthClientDetailsService;

    @Resource
    private OauthClientDetailsCustomMapper oauthClientDetailsCustomMapper;

    @Override
    public void insertOauthClientDetails(OauthClientDetailsEntity oauthClientDetailsEntity) {
        OauthClientDetails oauthClientDetails = OauthClientDetailsEntityConvertor.INSTANCE.toOauthClientDetails(oauthClientDetailsEntity);
        oauthClientDetails.setClientId(IdUtil.getSnowflake().nextIdStr());
        oauthClientDetailsService.save(oauthClientDetails);
    }

    @Override
    public void updateOauthClientDetails(OauthClientDetailsEntity oauthClientDetailsEntity) {
        OauthClientDetails oauthClientDetails = OauthClientDetailsEntityConvertor.INSTANCE.toOauthClientDetails(oauthClientDetailsEntity);
        oauthClientDetailsService.updateById(oauthClientDetails);
    }

    @Override
    public OauthClientDetailsEntity oauthClientDetailsCheck(String clientId) {
        // 这里查询领域模型所有数据信息时建议自己写查询方法
        OauthClientDetails oauthClientDetails = oauthClientDetailsService.getById(clientId);
        return OauthClientDetailsEntityConvertor.INSTANCE.toOauthClientDetailsEntity(oauthClientDetails);
    }

    @Override
    public void deleteOauthClientDetails(OauthClientDetailsEntity oauthClientDetailsEntity) {
        oauthClientDetailsService.removeById(oauthClientDetailsEntity.getClientId());
    }

    @Override
    public List<OauthClientDetailsEntity> queryOauthClientDetailsPageList(OauthClientDetailsQry oauthClientDetailsQry) {
        List<OauthClientDetailsDAO> daoList = oauthClientDetailsCustomMapper.queryOauthClientDetailsPageList(oauthClientDetailsQry);
        return OauthClientDetailsEntityConvertor.INSTANCE.toOauthClientDetailsEntityList(daoList);
    }
}
